package com.demo.adnroid.dummynote;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class NoteEdit extends Activity {
	private DB mDbHelper;
	
	@Override
	protected void onCreate (Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mDbHelper = new DB(this);
		mDbHelper.open();
		setContentView(R.layout.note_edit);
		findViews();
		showViews(savedInstanceState);
		
	}

	private EditText field_note;
	private Button button_confirm;
	
	private void findViews() {
		// TODO Auto-generated method stub
		field_note = (EditText) findViewById(R.id.note);
		button_confirm = (Button) findViewById(R.id.confirm);		
	}

	private Long mRowId;

	private void showViews(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		if (mRowId == null){
			Bundle extras = getIntent().getExtras();
			mRowId = extras != null ? extras.getLong(DB.KEY_ROWID) : null;
		}
		populateFields();
		button_confirm.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mDbHelper.update(mRowId, field_note.getText().toString());
				setResult(RESULT_OK);
				finish();
			}
		});
	}

	private void populateFields() {
		// TODO Auto-generated method stub
		if (mRowId != null){
			Cursor note = mDbHelper.get(mRowId);
			startManagingCursor(note);
			
			field_note.setText(note.getShort(note.getColumnIndexOrThrow(DB.KEY_NOTE)));
		}
	}
}
